#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import ConfigParser
#import xmllangs
import sys

debug = True

#LANGSXML_PATH=sys.path[0]+"/langs.xml"

class boxConfig:
	"""
		Configuration abstraction layer
		boxGUI will use following functions to handle configuration
		thus will not directly manipulate configuration dict
	"""	
	section = "options"
	files = [os.path.expanduser( "~/.suindara.conf" ), 
			sys.path[0]+"/suindara.conf"]
	actual_file = os.path.expanduser( "~/.suindara.conf" )
	options = {}	
			
	def load_config(self):
		"""
			Here we will be loading the configuration file to a dict
			dict being: options = {'section': []}
			[] list aquires the values in conf file (same order)
			if there is no options section in config file, it is created in dict
			and subsequently saved to conf file on exit
		"""
		config = ConfigParser.ConfigParser()
		config.read( self.files[0] )
		c = 0 # var for checking if options sections exists
		for section in config.sections():
			if section == 'options': c = 1
			if debug: print section
			if section == 'options':
				tmpData = [None,None,None,None,None,None,None,None,None]				
				for option in config.options(section):
					if debug: print " ", option, "=", config.get(section, option)
					if option == 'language': tmpData[0] = config.get(section, option)
					elif option == 'startuplogin': tmpData[1] = config.get(section, option)
					elif option == 'browserpath': tmpData[2] = config.get(section, option)
					elif option == 'autoconnect': tmpData[3] = int(config.get(section, option))
					elif option == 'publiccomputer': tmpData[4] = int(config.get(section, option))
					elif option == 'secureconnection': tmpData[5] = int(config.get(section, option))
					elif option == 'downloadsfolder': tmpData[6] = config.get(section, option)
					elif option == 'downloadsoverwrite': tmpData[7] = int(config.get(section, option))
					elif option == 'hidetotray': tmpData[8] = int(config.get(section, option))
				self.options['options'] = tmpData
			else:
				tmpData = []				
				for option in config.options(section):
					if debug: print " ", option, "=", config.get(section, option)
					tmpData.append(config.get(section, option))
				self.options[section] = tmpData				
		if c == 0:
			a = []
			a.append('english'), a.append('None'), a.append('firefox'), a.append(0), a.append(0), a.append(0), a.append(os.path.expanduser( "~/Desktop" )), a.append(1), a.append(0)
			self.options['options'] = a
		if debug: print self.options
				
	def get_option(self, section, key):
		"""
			Returns the n'th key in given section
			For options section use "get_goption"
		"""
		return self.options[section][key]

	def get_accounts(self):
		"""
			Returns a list of all accounts
		"""
		a = []
		for k in self.options.keys():
			if k != "options":
				a.append(k)
		return a

	def get_int_accounts(self):
		"""
			Return the number of accounts
		"""
		a = 0
		for k in self.options.keys():
			if k != "options":
				a+=1
		return a
		
	def get_int_option(self, section, key):
		return self.options[section][key]
		
	def set_option(self, section, key, value):
		"""
			Sets the value for n'th key in given section
			For options section use "set_goption"
		"""
		if( value!=None and value!="" and section != 'options'):
			try:
				a = self.options[section]
			except:
				a = [section, '', 0]
			a[key] = value
			self.options[section] = a
			if debug: print self.options
			
	def get_goption(self, key):
		"""
			Returns given key in options section
		"""
		a = self.options['options']
		return a[key]
			
	def set_goption(self, key, value):
		"""
			Sets value for n'th key in options section
		"""
		a = self.options['options']
		a[key] = value
		self.options['options'] = a
				
	def remove_option(self, section, key):
		"""
			Removes n'th key in given section
		"""
		self.options[section][key] = None
		
	def save_config(self):
		if debug: print '* options are: ' + str(self.options['options'])
		config = ConfigParser.ConfigParser()
		config.add_section('options')
		s = 1
		for r in self.options['options']:
			if debug: print '* r for %s is %s' % (s, r)
			if r == '': r = 'None'
			if s == 1: config.set('options', 'language', r)
			if s == 2: 
				if self.get_goption(4) == 0: config.set('options', 'startuplogin', r)
				elif self.get_goption(4) == 1: config.set('options', 'startuplogin', 'None')
			if s == 3: config.set('options', 'browserpath', r)
			if s == 4: config.set('options', 'autoconnect', r)
			if s == 5: config.set('options', 'publiccomputer', r)
			if s == 6: config.set('options', 'secureconnection', r)
			if s == 7: config.set('options', 'downloadsfolder', r)
			if s == 8: config.set('options', 'downloadsoverwrite', r)
			if s == 9: config.set('options', 'hidetotray', r)
			s+=1
		if self.get_goption(4) == 0:
			for k in self.get_accounts():
				config.add_section(k)
				s = 1
				for r in self.options[k]:
					if r == '': r = 'None'
					if s == 1: config.set(k, 'login', r)
					if s == 2: config.set(k, 'password', r)
					if s == 3: config.set(k, 'savelogin', r) 
					s+=1
		elif self.get_goption(4) == 1:
			for k in self.get_accounts():
				config.remove_section(k)
		config.write(open(self.files[0], "w"))

	def clear_accounts(self):
		"""
			Clears all accounts
		"""
		for section in self.get_accounts():
			a = self.options['options']
			self.options = {}
			self.options['options'] = a

